package com.labelimage.controller;


import cn.hutool.core.bean.BeanUtil;
import com.labelimage.bean.entity.LabelEntity;
import com.labelimage.bean.vo.LabelVo;
import com.labelimage.common.response.Result;
import com.labelimage.service.LabelService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("label")
@Validated
public class LabelController {
    @Resource
    private LabelService labelService;

    @GetMapping("/{templateId}")
    public Result<List<LabelVo>> get(@PathVariable("templateId") Long templateId) {
        return Result.OK(labelService.lambdaQuery()
                .eq(LabelEntity::getTemplateId, templateId)
                .list()
                .stream()
                .map(labelEntity -> {
                    LabelVo labelVo = LabelVo.builder().build();
                    BeanUtil.copyProperties(labelEntity, labelVo);
                    return labelVo;
                }).collect(Collectors.toList())
        );
    }
}
